﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.HVAC;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	public class SupplySystemDesign
	{
		public SupplySystemDesign(List<Element> listElem, Pipe conPipe, SupplyWaterConn supplyConn, UIApplication _uiApp)
		{
			this.listFI = new List<FamilyInstance>();
			this.sanitaryNum = 0;
			foreach (Element element in listElem)
			{
				if (element is FamilyInstance)
				{
					this.listFI.Add(element as FamilyInstance);
					this.sanitaryNum++;
				}
			}
			this.basePipe = conPipe;
			this.uiApp = _uiApp;
			this.doc = this.basePipe.Document;
			this.connStyle = supplyConn.ConnStyle;
			this.beDraw = supplyConn.BeDraw;
			this.beAdd = supplyConn.WaterMeter;
			this.mainPipeOffset = Convert.ToDouble(supplyConn.MainPipeOffset);
			this.valveOffset = Convert.ToDouble(supplyConn.ValveOffset);
			this.diameter = Convert.ToDouble(supplyConn.Diameter);
			this.sideDiameter = 20.0;
			this.angle = Convert.ToDouble(supplyConn.Angle);
			this.fiDesign = new PlumbSystemAutoDesign(this.doc);
			this.baseVector = this.fiDesign.GetPipeLineVector(this.angle * Math.PI / 180.0);
			this.moveDistance = 0.98425196850393692;
			this.systemType = this.fiDesign.GetPipeSystemType(this.basePipe);
			this.pipeType = this.fiDesign.GetPipeType(this.basePipe);
			this.IsHorFirst = true;
            this.beHotPipe = YJKRevitTools.BePipeSystemType(this.basePipe, (PipeSystemType)19);
		}

		public void AutoConnector()
		{
			this.DrawHorizonPipe();
		}

		private void DrawHorizonPipe()
		{
			YJKPipeConnectorHelper hypipeConnectorHelper = new YJKPipeConnectorHelper();
			this.checkPVDistance = true;
			bool beDesign = false;
			if (this.connStyle)
			{
				double num = Math.Abs(this.mainPipeOffset - this.valveOffset);
				if (num != 0.0 && num < this.sideDiameter * 2.5)
				{
					YJKMessageBox.Information("角阀到点标高与横管间的间距太小,\n请检查到点标高或管道标高。");
					this.checkPVDistance = false;
					return;
				}
				this.listFI = this.OrderFamilyInstance(this.basePipe, this.mainPipeOffset, this.listFI, ref this.originalPnt);
				beDesign = true;
				this.mainPipe = this.CreatHorizontalPipe(this.basePipe, this.mainPipeOffset, this.listFI, this.baseVector, this.diameter, 0.0);
				if (this.mainPipe == null)
				{
					throw new Exception("卫生设备出口连接口未找到，\n可能是由于出口Connector的流体走向不是'out'造成的。");
				}
				hypipeConnectorHelper.TPipeConnectRPipe(this.basePipe, this.mainPipe, this.doc, null, false, ref this.operationMSG, false, 0.0);
				this.hyRotate.MovePiping(this.mainPipe);
				this.doc.Regenerate();
				if (this.beAdd)
				{
					FamilySymbol familySymbolByName = this.GetFamilySymbolByName("水表", "Accessory", ref this.operationMSG);
					FamilySymbol familySymbolByName2 = this.GetFamilySymbolByName("止回阀", "PipeValve", ref this.operationMSG);
					if (familySymbolByName != null && familySymbolByName2 != null)
					{
						List<FamilySymbol> list = new List<FamilySymbol>();
						list.Add(familySymbolByName2);
						list.Add(familySymbolByName);
						GroupValueManage groupValueManage = new GroupValueManage(this.doc, this.mainPipe, this.originalPnt, 200.0, list);
						groupValueManage.dMinDis = 150.0;
						if (!groupValueManage.Go())
						{
							this.operationMSG += "阀件插入失败";
							return;
						}
						this.mainPipe = groupValueManage.GetEndPipe();
						this.doc.Regenerate();
					}
				}
			}
			else
			{
				this.mainPipe = this.basePipe;
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(this.mainPipe);
				double num2 = hypiping.StartConnector.Origin.Z - this.fiDesign.GetElevation(this.doc, 0.0);
				this.mainPipeOffset = num2 * 304.8;
				if (this.beDraw)
				{
					Connector connector = hypiping.StartConnector.Connector;
					Connector connector2 = hypiping.EndConnector.Connector;
					if (connector.IsConnected && connector2.IsConnected)
					{
						throw new Exception("管道已连接。");
					}
				}
				this.fiDesign.JudgeBeDrawPipe(this.basePipe, this.listFI, ref this.originalPnt);
				double num = Math.Abs(this.originalPnt.Z - this.fiDesign.GetElevation(this.doc, this.valveOffset));
				if (num != 0.0 && num < this.sideDiameter * 2.5 / 304.8)
				{
					YJKMessageBox.Information("角阀到点标高与横管间的间距太小,\n请检查到点标高或管道标高。");
					this.checkPVDistance = false;
					return;
				}
				this.listFI = this.OrderFamilyInstance(this.originalPnt, this.listFI);
				if (this.beDraw)
				{
					beDesign = true;
					this.mainPipe = this.CreatHorizontalPipe(this.basePipe, this.originalPnt, this.listFI, this.baseVector, this.diameter, 0.0);
					if (this.mainPipe == null)
					{
						throw new Exception("卫生设备出口连接口未找到，\n可能是由于出口Connector的流体走向不是'out'造成的。");
					}
					XYZ xyz = null;
					XYZ xyz2 = null;
					if (this.fiDesign.BeConnPipes(this.basePipe, this.mainPipe, this.originalPnt, ref xyz, ref xyz2))
					{
						hypipeConnectorHelper.creatELBOW(this.basePipe, this.mainPipe, this.originalPnt, this.doc);
					}
				}
				else
				{
					this.mainPipe = this.ExtendBasePipe(this.mainPipe, this.listFI, ref this.originalPnt);
				}
			}
			this.hyRotate.MovePiping(this.mainPipe);
			this.doc.Regenerate();
			int num3 = this.ConnSanitaryFixtures(beDesign);
			if (this.listFI.Count != this.sanitaryNum)
			{
				string str = string.Format("选择的卫浴中有{0}个未定义", this.sanitaryNum - this.listFI.Count);
				if (num3 > 0)
				{
					str = string.Format(str + "，有{0}个未定义连接点", num3);
				}
				str = string.Format(str + "，\n共{0}个卫浴设备未进行连接操作，请检查。", num3 + this.sanitaryNum - this.listFI.Count);
			}
		}

		private int ConnSanitaryFixtures(bool beDesign)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(this.mainPipe);
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			XYZ pipeVector = this.GetPipeVector(ref origin, ref origin2, hypiping);
			int result = 0;
			List<SupplyConnParameter> list = this.InitialParameter(this.mainPipe, this.originalPnt, pipeVector, this.listFI, ref result);
			this.doc.Regenerate();
			List<SupplyConnParameter> list2 = new List<SupplyConnParameter>();
			int num = 1;
			for (int i = 0; i < list.Count; i++)
			{
				int j;
				for (j = i; j < list.Count; j++)
				{
					if (list[j].IndexOfPipe != num)
					{
						i = j - 1;
						break;
					}
					list2.Add(list[j]);
				}
				if (i != j - 1)
				{
					i = j - 1;
				}
				bool beEnd = false;
				if (i == list.Count - 1)
				{
					beEnd = true;
				}
				if (list2.Count > 0)
				{
					if (list2.Count == 1)
					{
						this.ParamAndPipeConn(ref this.mainPipe, list2[0], ref this.operationMSG, beEnd, this.originalPnt, null);
					}
					else if (list2.Count > 1)
					{
						IList<List<SupplyConnParameter>> orderBranchParams = this.OrderByFanCoilOfVector(list2);
						this.ConnParamsAndPipe(ref this.mainPipe, orderBranchParams, ref this.operationMSG, beEnd);
					}
				}
				this.doc.Regenerate();
				list2.Clear();
				num++;
			}
			return result;
		}

		public Pipe ExtendBasePipe(Pipe pipe, List<FamilyInstance> listFI, ref XYZ point)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			Connector connector = hypiping.StartConnector.Connector;
			Connector connector2 = hypiping.EndConnector.Connector;
			if (connector.IsConnected && connector2.IsConnected)
			{
				return pipe;
			}
			XYZ xyz;
			XYZ xyz2;
			if (connector.IsConnected)
			{
				xyz = connector.Origin;
				xyz2 = this.GetExtendEndPoint(connector.Origin, connector2.Origin, listFI);
			}
			else if (connector2.IsConnected)
			{
				xyz = this.GetExtendEndPoint(connector2.Origin, connector.Origin, listFI);
				xyz2 = connector2.Origin;
			}
			else
			{
				List<XYZ> extendEndPoint = this.GetExtendEndPoint(listFI, connector.Origin, connector2.Origin);
				xyz = extendEndPoint[0];
				xyz2 = extendEndPoint[1];
			}
			Pipe pipe2 = hypiping.Extend(xyz, xyz2);
			new YJKRotateFamilyInstance().MovePiping(pipe2);
			double num = point.DistanceTo(xyz);
			double num2 = point.DistanceTo(xyz2);
			point = ((num < num2) ? xyz : xyz2);
			return pipe2;
		}

		private XYZ GetExtendEndPoint(XYZ startPnt, XYZ endPnt, List<FamilyInstance> listFI)
		{
			YJKLine hyline = new YJKLine(startPnt, endPnt);
			hyline.MakeUnBound();
			XYZ result = endPnt;
			List<FamilyInstance> source = this.OrderIntances(listFI, hyline);
			XYZ familyInstancePnt = this.GetFamilyInstancePnt(source.First<FamilyInstance>(), startPnt.Z);
			XYZ project = hyline.GetProject(familyInstancePnt);
			this.CheckAndExtendLine(project, ref startPnt, ref result, ref hyline, false);
			familyInstancePnt = this.GetFamilyInstancePnt(source.Last<FamilyInstance>(), startPnt.Z);
			project = hyline.GetProject(familyInstancePnt);
			this.CheckAndExtendLine(project, ref startPnt, ref result, ref hyline, false);
			return result;
		}

		private List<XYZ> GetExtendEndPoint(List<FamilyInstance> listFI, XYZ startPnt, XYZ endPnt)
		{
			List<XYZ> list = new List<XYZ>();
			YJKLine hyline = new YJKLine(startPnt, endPnt);
			hyline.MakeUnBound();
			XYZ item = startPnt;
			XYZ item2 = endPnt;
			List<FamilyInstance> source = this.OrderIntances(listFI, hyline);
			XYZ familyInstancePnt = this.GetFamilyInstancePnt(source.First<FamilyInstance>(), startPnt.Z);
			XYZ project = hyline.GetProject(familyInstancePnt);
			this.CheckAndExtendLine(project, ref item, ref item2, ref hyline, true);
			familyInstancePnt = this.GetFamilyInstancePnt(source.Last<FamilyInstance>(), startPnt.Z);
			project = hyline.GetProject(familyInstancePnt);
			this.CheckAndExtendLine(project, ref item, ref item2, ref hyline, true);
			list.Add(item);
			list.Add(item2);
			return list;
		}

		private void CheckAndExtendLine(XYZ basePnt, ref XYZ startPnt, ref XYZ endPnt, ref YJKLine hyLine, bool firstPntBeExtend = true)
		{
			if (hyLine.IsPointOnLineExtension(basePnt))
			{
				double num = basePnt.DistanceTo(startPnt);
				double num2 = basePnt.DistanceTo(endPnt);
				if (num < num2)
				{
					if (firstPntBeExtend)
					{
						startPnt = basePnt;
					}
				}
				else
				{
					endPnt = basePnt;
				}
				hyLine = new YJKLine(startPnt, endPnt);
				hyLine.MakeUnBound();
			}
		}

		private List<FamilyInstance> OrderIntances(List<FamilyInstance> listFI, YJKLine hyLine)
		{
			List<FamilyInstance> list = new List<FamilyInstance>();
			hyLine.MakeUnBound();
			Dictionary<FamilyInstance, double> dictionary = new Dictionary<FamilyInstance, double>();
			double z = hyLine.StartPoint.Z;
			XYZ xyz = null;
			for (int i = 0; i < listFI.Count; i++)
			{
				if (i == 0)
				{
					dictionary.Add(listFI[i], 0.0);
					xyz = this.GetFamilyInstancePnt(listFI[i], z);
				}
				else
				{
					double value = this.GetFamilyInstancePnt(listFI[i], z).DistanceTo(xyz);
					dictionary.Add(listFI[i], value);
				}
			}
			dictionary = (from r in dictionary
			orderby r.Value
			select r).ToDictionary((KeyValuePair<FamilyInstance, double> r) => r.Key, (KeyValuePair<FamilyInstance, double> r) => r.Value);
			FamilyInstance key = dictionary.Last<KeyValuePair<FamilyInstance, double>>().Key;
			xyz = this.GetFamilyInstancePnt(key, z);
			dictionary = new Dictionary<FamilyInstance, double>();
			foreach (FamilyInstance familyInstance in listFI)
			{
				double value2 = this.GetFamilyInstancePnt(familyInstance, z).DistanceTo(xyz);
				dictionary.Add(familyInstance, value2);
			}
			dictionary = (from r in dictionary
			orderby r.Value
			select r).ToDictionary((KeyValuePair<FamilyInstance, double> r) => r.Key, (KeyValuePair<FamilyInstance, double> r) => r.Value);
			foreach (KeyValuePair<FamilyInstance, double> keyValuePair in dictionary)
			{
				list.Add(keyValuePair.Key);
			}
			return list;
		}

		private List<FamilyInstance> OrderFamilyInstance(Pipe riserPipe, double pipeOffset, List<FamilyInstance> listFI, ref XYZ sPoint)
		{
			Document document = riserPipe.Document;
			double elevation = this.GetElevation(document, pipeOffset);
			XYZ pipeStartPnt = this.GetPipeStartPnt(riserPipe, elevation);
			sPoint = pipeStartPnt;
			Dictionary<FamilyInstance, double> dictionary = this.OrderFamilyInstanceList(listFI, pipeStartPnt);
			if (dictionary == null || dictionary.Count < 1)
			{
				throw new Exception("请检查卫生器具，可能没有进行卫浴定义。");
			}
			List<FamilyInstance> list = new List<FamilyInstance>();
			foreach (KeyValuePair<FamilyInstance, double> keyValuePair in dictionary)
			{
				list.Add(keyValuePair.Key);
			}
			return list;
		}

		private Pipe CreatHorizontalPipe(Pipe riserPipe, double pipeOffset, List<FamilyInstance> listFI, XYZ vector, double diameter, double slope)
		{
			Document document = riserPipe.Document;
			double elevation = this.GetElevation(document, pipeOffset);
			XYZ xyz = this.GetPipeStartPnt(riserPipe, elevation);
			PipingSystemType pipeSystemType = this.fiDesign.GetPipeSystemType(riserPipe);
			PipeType pt = this.fiDesign.GetPipeType(riserPipe);
			XYZ farthestPoint = this.GetFarthestPoint(listFI, xyz, vector, elevation);
			if (farthestPoint == null)
			{
				return null;
			}
			xyz += vector * 6.0 / 304.8;
			return YJKRevitTools.createPipe(0.0, xyz, farthestPoint, diameter, slope, pipeSystemType, pt, document);
		}

		private Pipe CreatHorizontalPipe(Pipe hPipe, XYZ pipePnt, List<FamilyInstance> listFI, XYZ vector, double diameter, double slope)
		{
			Document document = hPipe.Document;
			double z = pipePnt.Z;
			PipingSystemType pipeSystemType = this.fiDesign.GetPipeSystemType(hPipe);
			PipeType pt = this.fiDesign.GetPipeType(hPipe);
			XYZ farthestPoint = this.GetFarthestPoint(listFI, pipePnt, vector, z);
			if (farthestPoint == null)
			{
				return null;
			}
			return YJKRevitTools.createPipe(0.0, pipePnt, farthestPoint, diameter, slope, pipeSystemType, pt, document);
		}

		private XYZ GetFarthestPoint(List<FamilyInstance> listFI, XYZ startPnt, XYZ vector, double zValue)
		{
			XYZ xyz = null;
			YJKLine hyline = new YJKLine(startPnt, startPnt + vector);
			hyline.MakeUnBound();
			double num = 0.0;
			foreach (FamilyInstance elem in listFI)
			{
				SetSanitaryData setSanitaryData = YJKRevitTools.GetSetSanitaryData(this.doc, elem);
				if (setSanitaryData != null)
				{
					XYZ xyz2 = setSanitaryData.ColdPoint;
					if (this.beHotPipe)
					{
						xyz2 = setSanitaryData.HotPoint;
					}
					if (xyz2 != null)
					{
						XYZ point = xyz2;
						XYZ project = hyline.GetProject(point);
						if (project.DistanceTo(startPnt) > num)
						{
							xyz = project;
							num = project.DistanceTo(startPnt);
						}
					}
				}
			}
			if (xyz == null)
			{
				return xyz;
			}
			XYZ xyz3 = xyz.Subtract(startPnt).Normalize();
			xyz = xyz.Subtract(xyz3 * 20.0 / 304.8);
			return xyz;
		}

		private List<FamilyInstance> OrderFamilyInstance(XYZ startPnt, List<FamilyInstance> listFI)
		{
			Dictionary<FamilyInstance, double> dictionary = this.OrderFamilyInstanceList(listFI, startPnt);
			if (dictionary == null || dictionary.Count < 1)
			{
				throw new Exception("请检查卫生器具，可能没有进行卫浴定义。");
			}
			List<FamilyInstance> list = new List<FamilyInstance>();
			foreach (KeyValuePair<FamilyInstance, double> keyValuePair in dictionary)
			{
				list.Add(keyValuePair.Key);
			}
			return list;
		}

		private Dictionary<FamilyInstance, double> OrderFamilyInstanceList(List<FamilyInstance> listFI, XYZ pnt)
		{
			new List<FamilyInstance>();
			Dictionary<FamilyInstance, double> dictionary = new Dictionary<FamilyInstance, double>();
			foreach (FamilyInstance familyInstance in listFI)
			{
				SetSanitaryData setSanitaryData = YJKRevitTools.GetSetSanitaryData(this.doc, familyInstance);
				if (setSanitaryData != null)
				{
					XYZ xyz = setSanitaryData.ColdPoint;
					if (this.beHotPipe)
					{
						xyz = setSanitaryData.HotPoint;
					}
					if (xyz != null)
					{
						dictionary.Add(familyInstance, pnt.DistanceTo(xyz));
					}
				}
			}
			dictionary = (from r in dictionary
			orderby r.Value
			select r).ToDictionary((KeyValuePair<FamilyInstance, double> r) => r.Key, (KeyValuePair<FamilyInstance, double> r) => r.Value);
			return dictionary;
		}

		private double GetElevation(Autodesk.Revit.DB.Document doc, double pipeOffset)
		{
			return doc.ActiveView.GenLevel.Elevation + pipeOffset / 304.8;
		}

		private XYZ GetPipeStartPnt(Pipe pipe, double zValue)
		{
			XYZ result = null;
			if (YJKRevitTools.isRiserPipe(pipe))
			{
				XYZ xyz = YJKRevitTools.GetElementEndPoint(pipe)[0];
				result = new XYZ(xyz.X, xyz.Y, zValue);
			}
			return result;
		}

		private void ConnectParamAndPipe(ref Pipe pipe, SupplyConnParameter fcParam, XYZ basePnt, ref string connMSG)
		{
			if (pipe == null)
			{
				return;
			}
			Pipe pipe2 = null;
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			if (hypiping.StartConnector.Origin.DistanceTo(this.originalPnt) > hypiping.EndConnector.Origin.DistanceTo(this.originalPnt))
			{
				origin = hypiping.EndConnector.Origin;
				origin2 = hypiping.StartConnector.Origin;
			}
			double num = hypiping.GetDiameter() * 2.0;
			if (origin.DistanceTo(fcParam.ProPoint) > num)
			{
				HYPipeConnect hypipeConnect = new HYPipeConnect();
				Pipe pipe3 = null;
				double num2 = fcParam.ProPoint.DistanceTo(fcParam.TurnPoint);
				double num3 = (hypiping.GetDiameter() + this.sideDiameter / 304.8) * 1.5;
				if (num2 <= num3)
				{
					pipe3 = this.ConnectAngleValue(pipe, fcParam, false);
				}
				else if (num2 > num3)
				{
					Pipe pipe4 = this.ConnectAngleValue(pipe, fcParam, true);
					if (pipe4 != null)
					{
						pipe3 = this.CreatSidePipe(fcParam.ProPoint, fcParam.TurnPoint, this.sideDiameter);
						this.doc.Regenerate();
						this.hyRotate.MovePiping(pipe3);
						hypipeConnect.NewElbowFitting(new List<Pipe>
						{
							pipe4,
							pipe3
						}, true);
					}
				}
				if (pipe3 == null)
				{
					connMSG += "支管太短，没有办法进行绘制。";
					return;
				}
				this.hyRotate.MovePiping(pipe3);
				if (origin2.DistanceTo(fcParam.ProPoint) > num)
				{
					IList<Pipe> list = hypiping.Separate(fcParam.ProPoint);
					this.doc.Regenerate();
					this.hyRotate.MovePiping(list[0]);
					this.hyRotate.MovePiping(list[1]);
					XYZ pipeMidPnt = this.GetPipeMidPnt(list[0]);
					XYZ pipeMidPnt2 = this.GetPipeMidPnt(list[1]);
					if (pipeMidPnt.DistanceTo(basePnt) > pipeMidPnt2.DistanceTo(basePnt))
					{
						pipe2 = list[0];
					}
					else
					{
						pipe2 = list[1];
					}
					list.Add(pipe3);
					hypipeConnect.NewTeeFitting(ref list, true);
					this.doc.Regenerate();
				}
				else
				{
					hypipeConnect.NewElbowFitting(new List<Pipe>
					{
						pipe,
						pipe3
					}, true);
					this.doc.Regenerate();
					pipe2 = null;
				}
			}
			pipe = pipe2;
		}

		private XYZ GetBathroomOutPoint(Element el)
		{
			XYZ xyz = XYZ.Zero;
			Connector elementOutConnector = YJKRevitTools.GetElementOutConnector(el, (Autodesk.Revit.DB.Domain)3);
			if (elementOutConnector == null)
			{
				return null;
			}
			xyz = elementOutConnector.Origin;
			if (el.Document.IsLinked)
			{
				xyz = this.doc.GetLinkedDocument(el.Id).GetPointInLinkDocument(xyz);
			}
			return xyz;
		}

		private XYZ GetSupplyVector(Element el)
		{
			if (!(el is FamilyInstance))
			{
				return null;
			}
			XYZ xyz = XYZ.BasisX;
			XYZ xyz2 = (this.GetBathroomOutPoint(el) == null) ? null : new XYZ(this.GetBathroomOutPoint(el).X, this.GetBathroomOutPoint(el).Y, 0.0);
			XYZ xyz3 = (el as FamilyInstance).FacingOrientation.Normalize();
			XYZ xyz4 = YJKRevitTools.Rotate(xyz3, new XYZ(0.0, 0.0, 1.0), Math.PI*.5).Normalize();
			SetSanitaryData setSanitaryData = YJKRevitTools.GetSetSanitaryData(this.doc, el);
			if (xyz2 == null || setSanitaryData == null || (setSanitaryData.ColdPoint == null && setSanitaryData.HotPoint == null))
			{
				return xyz3;
			}
			XYZ xyz5 = (setSanitaryData.ColdPoint == null) ? null : new XYZ(setSanitaryData.ColdPoint.X, setSanitaryData.ColdPoint.Y, 0.0);
			XYZ xyz6 = (setSanitaryData.HotPoint == null) ? null : new XYZ(setSanitaryData.HotPoint.X, setSanitaryData.HotPoint.Y, 0.0);
			if ((xyz5 == null && xyz6 != null) || (xyz5 != null && xyz6 == null) || xyz5.DistanceTo(xyz6) < 0.0001)
			{
				XYZ xyz7 = xyz5;
				if (xyz7 == null)
				{
					xyz7 = xyz6;
				}
				YJKLine hyline = new YJKLine(xyz2, xyz2 + xyz3);
				YJKLine hyline2 = new YJKLine(xyz2, xyz2 + xyz4);
				hyline.MakeUnBound();
				hyline2.MakeUnBound();
				if (xyz7.DistanceTo(hyline.GetProject(xyz7)) > xyz7.DistanceTo(hyline2.GetProject(xyz7)))
				{
					xyz = hyline.GetProject(xyz7).Subtract(xyz7);
				}
				else
				{
					xyz = hyline2.GetProject(xyz7).Subtract(xyz7);
				}
			}
			else if (xyz5 != null && xyz6 != null)
			{
				YJKLine hyline3 = new YJKLine(xyz5, xyz6);
				if (hyline3.IsPointOnCurveLine(xyz2))
				{
					xyz = xyz3;
				}
				else
				{
					xyz = xyz2.Subtract(hyline3.GetProject(xyz2));
				}
			}
			else
			{
				xyz = xyz3;
			}
			if (xyz.IsAlmostEqualTo(new XYZ(0.0, 0.0, 0.0), 0.0001))
			{
				xyz = xyz3;
			}
			return xyz;
		}

		private void ParamAndPipeConn(ref Pipe pipe, SupplyConnParameter fcParam, ref string connMSG, bool beEnd, XYZ basePnt, XYZ sideVector = null)
		{
			if (pipe == null)
			{
				return;
			}
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			if (hypiping.StartConnector.Origin.DistanceTo(basePnt) > hypiping.EndConnector.Origin.DistanceTo(basePnt))
			{
				origin = hypiping.EndConnector.Origin;
				origin2 = hypiping.StartConnector.Origin;
			}
			hypiping.GetDiameter();
			HYPipeConnect hypipeConnect = new HYPipeConnect();
			double num = (hypiping.GetDiameter() + this.sideDiameter / 304.8) * 1.6;
			double num2 = Math.Abs(fcParam.LayPoint.Z - fcParam.TurnPoint.Z);
			double num3 = fcParam.ProPoint.DistanceTo(fcParam.TurnPoint);
			XYZ xyz = this.GetSupplyVector(fcParam.Instance);
			XYZ xyz2 = new XYZ(fcParam.ProPoint.X, fcParam.ProPoint.Y, fcParam.LayPoint.Z);
			if (sideVector != null)
			{
				xyz = sideVector;
			}
			origin.Subtract(origin2).Normalize();
			Connector connAngle = null;
			FamilyInstance familyInstance = this.LayAngleValue(fcParam.LayPoint, fcParam.Instance, xyz, ref connAngle, fcParam.BIsUp);
			Pipe pipe3;
			if (num2 < num || num3 < num)
			{
				XYZ xyz3;
				XYZ proPoint;
				if (num2 < num)
				{
					xyz3 = fcParam.TurnPoint;
					proPoint = fcParam.ProPoint;
					if (num3 < num)
					{
						xyz3 = proPoint + num * xyz;
					}
				}
				else
				{
					proPoint = fcParam.ProPoint;
					xyz3 = new XYZ(proPoint.X, proPoint.Y, fcParam.LayPoint.Z);
				}
				Pipe pipe2 = this.CreatSidePipe(xyz3, proPoint, this.sideDiameter);
				this.hyRotate.MovePiping(pipe2);
				this.doc.Regenerate();
				this.hyRotate.MoveElem(familyInstance, xyz3);
				this.PipeConnectAngleValve(pipe2, xyz3, xyz, familyInstance, connAngle);
				pipe3 = this.ConnSideAndMainPipe(pipe2, pipe, fcParam.ProPoint, beEnd, basePnt);
				pipe = pipe3;
				return;
			}
			XYZ layPoint = fcParam.LayPoint;
			XYZ xyz4 = fcParam.TurnPoint;
			if (!this.IsHorFirst)
			{
				xyz4 = xyz2;
			}
			Pipe pipe4 = this.CreatSidePipe(layPoint, xyz4, this.sideDiameter);
			this.hyRotate.MovePiping(pipe4);
			this.doc.Regenerate();
			this.PipeConnectAngleValve(pipe4, layPoint, xyz, familyInstance, connAngle);
			Pipe sidePipe = pipe4;
			if (num3 >= num)
			{
				Pipe pipe5 = this.CreatSidePipe(xyz4, fcParam.ProPoint, this.sideDiameter);
				this.hyRotate.MovePiping(pipe5);
				this.doc.Regenerate();
				hypipeConnect.NewElbowFitting(new List<Pipe>
				{
					pipe5,
					pipe4
				}, true);
				sidePipe = pipe5;
			}
			pipe3 = this.ConnSideAndMainPipe(sidePipe, pipe, fcParam.ProPoint, beEnd, basePnt);
			pipe = pipe3;
		}

		private void PipeConnectAngleValve(Pipe pi, XYZ ConnectPnt, XYZ rorateVector, FamilyInstance fsAngle, Connector connAngle)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pi);
			bool flag = false;
			double num = rorateVector.AngleTo(hypiping.StartConnector.Origin - hypiping.EndConnector.Origin);
			if (Math.Abs(num - Math.PI) < 0.0001 || num < 0.0001)
			{
				flag = true;
			}
			if (flag)
			{
				Connector connector = hypiping.StartConnector.Connector;
				if (hypiping.StartConnector.Origin.DistanceTo(ConnectPnt) > hypiping.EndConnector.Origin.DistanceTo(ConnectPnt))
				{
					connector = hypiping.EndConnector.Connector;
				}
				this.hyRotate.MoveElem(connAngle.Origin.Subtract(connector.Origin), fsAngle);
				connector.ConnectTo(connAngle);
				this.hyRotate.MoveFamilyInstance(fsAngle, connAngle.Origin.Subtract(connector.Origin) * 0.001);
				return;
			}
			XYZ endPnt = ConnectPnt + 2.0 * rorateVector;
			Pipe pipe = this.CreatSidePipe(ConnectPnt, endPnt, this.sideDiameter);
			this.hyRotate.MovePiping(pipe);
			this.doc.Regenerate();
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe);
			Connector appropriateConnector = hypiping.GetAppropriateConnector(ConnectPnt);
			Connector appropriateConnector2 = hypiping2.GetAppropriateConnector(ConnectPnt);
			try
			{
				FamilyInstance fi = this.doc.Create.NewElbowFitting(appropriateConnector, appropriateConnector2);
				this.hyRotate.MoveFamilyInstance(fi, new XYZ(0.0, 0.0, 0.0001));
				Connector connectedConnector = appropriateConnector2.GetConnectedConnector();
				this.doc.DeleteElement(pipe);
				this.doc.Regenerate();
				XYZ moveVector = connectedConnector.Origin.Subtract(connAngle.Origin);
				this.hyRotate.MoveElem(moveVector, fsAngle);
				if (connectedConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(connAngle.CoordinateSystem.BasisZ * -1.0, 0.0001))
				{
					connectedConnector.ConnectTo(connAngle);
				}
				this.doc.Regenerate();
			}
			catch (Exception)
			{
				throw new Exception("角阀到点标高与横管间的间距太小,\n请检查到点标高或管道标高！");
			}
		}

		private Pipe ConnSideAndMainPipe(Pipe sidePipe, Pipe mainPipe, XYZ intersPnt, bool beEnd, XYZ basePnt)
		{
			HYPipeConnect hypipeConnect = new HYPipeConnect();
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(mainPipe);
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			if (hypiping.StartConnector.Origin.DistanceTo(basePnt) > hypiping.EndConnector.Origin.DistanceTo(basePnt))
			{
				origin = hypiping.EndConnector.Origin;
				origin2 = hypiping.StartConnector.Origin;
			}
			double num = hypiping.GetDiameter() * 2.0;
			Pipe result;
			if (beEnd && (hypiping.IsPointOnPipingCurveExtension(intersPnt) || origin2.DistanceTo(intersPnt) <= num))
			{
				hypipeConnect.NewElbowFitting(new List<Pipe>
				{
					mainPipe,
					sidePipe
				}, true);
				result = mainPipe;
			}
			else if (origin2.DistanceTo(intersPnt) > num && origin.DistanceTo(intersPnt) > num)
			{
				IList<Pipe> list = hypiping.Separate(intersPnt);
				if (list == null || list.Count < 2)
				{
					return mainPipe;
				}
				this.doc.Regenerate();
				this.hyRotate.MovePiping(list[0]);
				this.hyRotate.MovePiping(list[1]);
				XYZ pipeMidPnt = this.GetPipeMidPnt(list[0]);
				XYZ pipeMidPnt2 = this.GetPipeMidPnt(list[1]);
				if (pipeMidPnt.DistanceTo(basePnt) > pipeMidPnt2.DistanceTo(basePnt))
				{
					result = list[0];
				}
				else
				{
					result = list[1];
				}
				list.Add(sidePipe);
				hypipeConnect.NewTeeFitting(ref list, true);
				this.doc.Regenerate();
			}
			else
			{
				hypipeConnect.NewElbowFitting(new List<Pipe>
				{
					mainPipe,
					sidePipe
				}, true);
				this.doc.Regenerate();
				result = mainPipe;
			}
			return result;
		}

		private XYZ GetLayPntToPipeVector(FamilyInstance fi, XYZ spPoint, XYZ epPoint)
		{
			XYZ xyz = (fi.Location as LocationPoint).Point;
			YJKLine hyline = new YJKLine(spPoint, epPoint);
			hyline.MakeUnBound();
			XYZ project = hyline.GetProject(xyz);
			xyz = new XYZ(xyz.X, xyz.Y, project.Z);
			return project.Subtract(xyz).Normalize();
		}

		private Connector GetNotConnConn(FamilyInstance fi)
		{
			List<Connector> elementAllConnector = YJKRevitTools.GetElementAllConnector(fi);
			Connector result = null;
			foreach (Connector connector in elementAllConnector)
			{
				if (!connector.IsConnected)
				{
					result = connector;
					break;
				}
			}
			return result;
		}

		private void ConnParamAndPipe(ref Pipe pipe, SupplyConnParameter fcParam, ref string connMSG, bool beEnd)
		{
			if (pipe == null)
			{
				return;
			}
			Pipe pipe2 = null;
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			if (hypiping.StartConnector.Origin.DistanceTo(this.originalPnt) > hypiping.EndConnector.Origin.DistanceTo(this.originalPnt))
			{
				origin = hypiping.EndConnector.Origin;
				origin2 = hypiping.StartConnector.Origin;
			}
			double num = hypiping.GetDiameter() * 2.0;
			if (origin.DistanceTo(fcParam.ProPoint) > num)
			{
				new YJKPipeConnectorHelper();
				HYPipeConnect hypipeConnect = new HYPipeConnect();
				Pipe pipe3 = null;
				double num2 = fcParam.ProPoint.DistanceTo(fcParam.TurnPoint);
				double num3 = (hypiping.GetDiameter() + this.sideDiameter / 304.8) * 1.5;
				if (num2 <= num3)
				{
					pipe3 = this.ConnectAngleValue(pipe, fcParam, false);
				}
				else if (num2 > num3)
				{
					Pipe pipe4 = this.ConnectAngleValue(pipe, fcParam, true);
					if (pipe4 != null)
					{
						pipe3 = this.CreatSidePipe(fcParam.ProPoint, fcParam.TurnPoint, this.sideDiameter);
						this.doc.Regenerate();
						this.hyRotate.MovePiping(pipe3);
						hypipeConnect.NewElbowFitting(new List<Pipe>
						{
							pipe4,
							pipe3
						}, true);
					}
				}
				if (pipe3 == null)
				{
					connMSG += "支管太短，没有办法进行绘制。";
					return;
				}
				this.hyRotate.MovePiping(pipe3);
				if (beEnd)
				{
					hypipeConnect.NewElbowFitting(new List<Pipe>
					{
						pipe,
						pipe3
					}, true);
					pipe2 = null;
				}
				else if (origin2.DistanceTo(fcParam.ProPoint) > num)
				{
					IList<Pipe> list = hypiping.Separate(fcParam.ProPoint);
					this.doc.Regenerate();
					this.hyRotate.MovePiping(list[0]);
					this.hyRotate.MovePiping(list[1]);
					XYZ pipeMidPnt = this.GetPipeMidPnt(list[0]);
					XYZ pipeMidPnt2 = this.GetPipeMidPnt(list[1]);
					if (pipeMidPnt.DistanceTo(this.originalPnt) > pipeMidPnt2.DistanceTo(this.originalPnt))
					{
						pipe2 = list[0];
					}
					else
					{
						pipe2 = list[1];
					}
					list.Add(pipe3);
					hypipeConnect.NewTeeFitting(ref list, true);
					this.doc.Regenerate();
				}
				else
				{
					hypipeConnect.NewElbowFitting(new List<Pipe>
					{
						pipe,
						pipe3
					}, true);
					this.doc.Regenerate();
					pipe2 = null;
				}
			}
			pipe = pipe2;
		}

		private void ConnParamsAndPipe(ref Pipe pipe, IList<List<SupplyConnParameter>> orderBranchParams, ref string connMSG, bool beEnd)
		{
			if (pipe == null)
			{
				return;
			}
			Pipe pipe2 = null;
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			if (hypiping.StartConnector.Origin.DistanceTo(this.originalPnt) > hypiping.EndConnector.Origin.DistanceTo(this.originalPnt))
			{
				origin = hypiping.EndConnector.Origin;
				origin2 = hypiping.StartConnector.Origin;
			}
			double num = hypiping.GetDiameter() * 2.0;
			HYPipeConnect hypipeConnect = new HYPipeConnect();
			SupplyConnParameter supplyConnParameter = orderBranchParams[0][0];
			SupplyConnParameter supplyConnParameter2 = null;
			if (orderBranchParams.Count > 1)
			{
				supplyConnParameter2 = orderBranchParams[1][0];
			}
			if (origin.DistanceTo(supplyConnParameter.ProPoint) > num)
			{
				Pipe pipe3 = null;
				if (supplyConnParameter.ProPoint.DistanceTo(supplyConnParameter.TurnPoint) > 0.98425196850393692)
				{
					pipe3 = this.CreatSidePipe(supplyConnParameter.ProPoint, supplyConnParameter.TurnPoint, this.sideDiameter);
					this.hyRotate.MovePiping(pipe3);
				}
				Pipe pipe4 = null;
				if (supplyConnParameter2 != null)
				{
					XYZ xyz = supplyConnParameter2.TurnPoint.Subtract(supplyConnParameter2.ProPoint);
					supplyConnParameter2.HorizontalVector.GetLength();
					if (xyz.GetLength() > 0.98425196850393692)
					{
						pipe4 = this.CreatSidePipe(supplyConnParameter.ProPoint, supplyConnParameter.ProPoint.Add(xyz), this.sideDiameter);
						this.hyRotate.MovePiping(pipe4);
					}
					if (pipe3 == null && pipe4 == null)
					{
						connMSG += "支管太短，没有办法进行绘制。";
						return;
					}
				}
				if (beEnd)
				{
					IList<Pipe> list = new List<Pipe>();
					list.Add(pipe);
					if (pipe3 != null)
					{
						list.Add(pipe3);
					}
					if (pipe4 != null)
					{
						list.Add(pipe4);
					}
					if (list.Count == 3)
					{
						hypipeConnect.NewTeeFitting(ref list, true);
					}
					else if (list.Count == 2)
					{
						new YJKPipeConnectorHelper().creatELBOW(pipe, pipe3, supplyConnParameter.ProPoint, this.doc);
					}
					pipe2 = null;
				}
				else if (origin2.DistanceTo(supplyConnParameter.ProPoint) > num)
				{
					IList<Pipe> list2 = hypiping.Separate(supplyConnParameter.ProPoint);
					this.hyRotate.MovePiping(list2[0]);
					this.hyRotate.MovePiping(list2[1]);
					XYZ pipeMidPnt = this.GetPipeMidPnt(list2[0]);
					XYZ pipeMidPnt2 = this.GetPipeMidPnt(list2[1]);
					if (pipeMidPnt.DistanceTo(this.originalPnt) > pipeMidPnt2.DistanceTo(this.originalPnt))
					{
						pipe2 = list2[0];
					}
					else
					{
						pipe2 = list2[1];
					}
					if (pipe3 != null)
					{
						list2.Add(pipe3);
					}
					if (pipe4 != null)
					{
						list2.Add(pipe4);
					}
					if (list2.Count == 3)
					{
						hypipeConnect.NewTeeFitting(ref list2, true);
					}
					else if (list2.Count == 4)
					{
						hypipeConnect.NewCrossFitting(ref list2, true);
					}
				}
				else
				{
					IList<Pipe> list3 = new List<Pipe>();
					list3.Add(pipe);
					if (pipe3 != null)
					{
						list3.Add(pipe3);
					}
					if (pipe4 != null)
					{
						list3.Add(pipe4);
					}
					if (list3.Count == 3)
					{
						hypipeConnect.NewTeeFitting(ref list3, true);
					}
					else if (list3.Count == 2)
					{
						new YJKPipeConnectorHelper().creatELBOW(pipe, pipe3, supplyConnParameter.ProPoint, this.doc);
					}
					pipe2 = null;
				}
				XYZ sideVector = supplyConnParameter.ProPoint.Subtract(this.originalPnt).Normalize();
				if (pipe3 != null)
				{
					List<SupplyConnParameter> list4 = this.ReInitialConnParamters(pipe3, orderBranchParams[0]);
					for (int i = 0; i < list4.Count; i++)
					{
						this.ParamAndPipeConn(ref pipe3, list4[i], ref this.operationMSG, false, supplyConnParameter.ProPoint, sideVector);
					}
				}
				if (pipe4 != null)
				{
					List<SupplyConnParameter> list5 = this.ReInitialConnParamters(pipe4, orderBranchParams[1]);
					for (int j = 0; j < orderBranchParams[1].Count; j++)
					{
						if (j == orderBranchParams[1].Count - 1)
						{
							this.ParamAndPipeConn(ref pipe4, list5[j], ref this.operationMSG, false, supplyConnParameter.ProPoint, sideVector);
						}
						else
						{
							this.ParamAndPipeConn(ref pipe4, list5[j], ref this.operationMSG, false, supplyConnParameter.ProPoint, sideVector);
						}
					}
				}
			}
			pipe = pipe2;
		}

		private List<SupplyConnParameter> ReInitialConnParamters(Pipe pipe, List<SupplyConnParameter> fcParams)
		{
			List<SupplyConnParameter> list = new List<SupplyConnParameter>();
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			XYZ origin = hypiping.EndConnector.Origin;
			XYZ origin2 = hypiping.StartConnector.Origin;
			YJKLine hyline = new YJKLine(origin, origin2);
			hyline.MakeUnBound();
			XYZ proPoint = fcParams[0].ProPoint;
			foreach (SupplyConnParameter supplyConnParameter in fcParams)
			{
				SupplyConnParameter supplyConnParameter2 = new SupplyConnParameter();
				supplyConnParameter2.Instance = supplyConnParameter.Instance;
				supplyConnParameter2.LayPoint = supplyConnParameter.LayPoint;
				supplyConnParameter2.TurnPoint = new XYZ(supplyConnParameter2.LayPoint.X, supplyConnParameter2.LayPoint.Y, origin.Z);
				supplyConnParameter2.ProPoint = hyline.GetProject(supplyConnParameter2.TurnPoint);
				supplyConnParameter2.DistanceOfEnd = supplyConnParameter.ProPoint.DistanceTo(proPoint);
				supplyConnParameter2.HorizontalVector = supplyConnParameter.TurnPoint.Subtract(supplyConnParameter.ProPoint);
				list.Add(supplyConnParameter2);
			}
			return (from c in list
			orderby c.DistanceOfEnd descending
			select c).ToList<SupplyConnParameter>();
		}

		private List<SupplyConnParameter> ReInitialConnParams(Pipe pipe, List<SupplyConnParameter> fcParams, XYZ baseVector)
		{
			List<SupplyConnParameter> list = new List<SupplyConnParameter>();
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			YJKLine hyline = new YJKLine(hypiping.EndConnector.Origin, hypiping.StartConnector.Origin);
			hyline.MakeUnBound();
			foreach (SupplyConnParameter supplyConnParameter in fcParams)
			{
				SupplyConnParameter supplyConnParameter2 = new SupplyConnParameter();
				supplyConnParameter2.Instance = supplyConnParameter.Instance;
				supplyConnParameter2.TurnPoint = supplyConnParameter.LayPoint.Subtract(baseVector * this.moveDistance / 2.0);
				supplyConnParameter2.ProPoint = hyline.GetProject(supplyConnParameter2.TurnPoint);
				list.Add(supplyConnParameter2);
			}
			return list;
		}

		private XYZ GetFamilyInstancePnt(FamilyInstance fi)
		{
			SetSanitaryData setSanitaryData = YJKRevitTools.GetSetSanitaryData(this.doc, fi);
			if (setSanitaryData == null)
			{
				return null;
			}
			XYZ xyz = setSanitaryData.ColdPoint;
			if (this.beHotPipe)
			{
				xyz = setSanitaryData.HotPoint;
			}
			if (xyz == null)
			{
				return null;
			}
			return xyz;
		}

		private XYZ GetFamilyInstancePnt(FamilyInstance fi, double zValue)
		{
			SetSanitaryData setSanitaryData = YJKRevitTools.GetSetSanitaryData(this.doc, fi);
			if (setSanitaryData == null)
			{
				return null;
			}
			XYZ xyz = setSanitaryData.ColdPoint;
			if (this.beHotPipe)
			{
				xyz = setSanitaryData.HotPoint;
			}
			if (xyz == null)
			{
				return null;
			}
			return new XYZ(xyz.X, xyz.Y, zValue);
		}

		private void ExtendPipe(ref Pipe pipe, FamilyInstance fi)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			XYZ xyz = hypiping.StartConnector.Origin;
			XYZ xyz2 = hypiping.EndConnector.Origin;
			YJKLine hyline = new YJKLine(xyz, xyz2);
			hyline.MakeUnBound();
			XYZ familyInstancePnt = this.GetFamilyInstancePnt(fi);
			if (familyInstancePnt == null)
			{
				return;
			}
			XYZ project = hyline.GetProject(familyInstancePnt);
			if (hypiping.IsPointOnPipingCurveExtension(project))
			{
				bool flag = true;
				if (xyz.DistanceTo(project) < xyz2.DistanceTo(project))
				{
					XYZ xyz3 = xyz2.Subtract(xyz).Normalize();
					xyz = project + xyz3 * this.moveDistance;
					if (hypiping.StartConnector.Connector.IsConnected)
					{
						flag = false;
					}
				}
				else
				{
					XYZ xyz3 = xyz.Subtract(xyz2).Normalize();
					xyz2 = project + xyz3 * this.moveDistance;
					if (hypiping.EndConnector.Connector.IsConnected)
					{
						flag = false;
					}
				}
				if (flag)
				{
					hypiping.Extend(xyz, xyz2);
				}
			}
		}

		private void ConnectPipeParams(ref Pipe pipe, SupplyConnParameter fcParam, ref string connMSG, XYZ basePnt)
		{
		}

		private void ConnPipeParams(ref Pipe pipe, SupplyConnParameter fcParam, ref string connMSG, XYZ basePnt, bool beEnd)
		{
			if (pipe == null || beEnd)
			{
				return;
			}
			Pipe pipe2 = null;
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			if (hypiping.StartConnector.Origin.DistanceTo(basePnt) > hypiping.EndConnector.Origin.DistanceTo(basePnt))
			{
				origin = hypiping.EndConnector.Origin;
				origin2 = hypiping.StartConnector.Origin;
			}
			double num = hypiping.GetDiameter() * 2.0;
			if (origin.DistanceTo(fcParam.ProPoint) > num)
			{
				YJKPipeConnectorHelper hypipeConnectorHelper = new YJKPipeConnectorHelper();
				HYPipeConnect hypipeConnect = new HYPipeConnect();
				Pipe pipe3 = null;
				if (fcParam.ProPoint.DistanceTo(fcParam.TurnPoint) >= 0.49212598425196846)
				{
					pipe3 = this.CreatSidePipe(fcParam.ProPoint, fcParam.TurnPoint, this.sideDiameter);
				}
				if (pipe3 == null)
				{
					connMSG += "支管太短，没有办法进行绘制。";
					return;
				}
				this.hyRotate.MovePiping(pipe3);
				this.ConnAngleValve(pipe3, fcParam.TurnPoint, fcParam);
				if (origin2.DistanceTo(fcParam.ProPoint) > num)
				{
					IList<Pipe> list = hypiping.Separate(fcParam.ProPoint);
					this.doc.Regenerate();
					this.hyRotate.MovePiping(list[0]);
					this.hyRotate.MovePiping(list[1]);
					XYZ pipeMidPnt = this.GetPipeMidPnt(list[0]);
					XYZ pipeMidPnt2 = this.GetPipeMidPnt(list[1]);
					if (pipeMidPnt.DistanceTo(basePnt) < pipeMidPnt2.DistanceTo(basePnt))
					{
						pipe2 = list[0];
					}
					else
					{
						pipe2 = list[1];
					}
					list.Add(pipe3);
					hypipeConnect.NewTeeFitting(ref list, true);
					this.doc.Regenerate();
				}
				else
				{
					List<Pipe> list2 = new List<Pipe>();
					((ICollection<Pipe>)list2).Add(pipe);
					((ICollection<Pipe>)list2).Add(pipe3);
					hypipeConnectorHelper.creatELBOW(pipe, pipe3, fcParam.ProPoint, this.doc);
					this.doc.Regenerate();
					pipe2 = null;
				}
			}
			pipe = pipe2;
		}

		private Pipe CreatSidePipe(XYZ startPnt, XYZ endPnt, double diameter)
		{
			return YJKRevitTools.createPipe(0.0, startPnt, endPnt, diameter, 0.0, this.systemType, this.pipeType, this.doc);
		}

		private void ConnAngleValve(Pipe pipe, XYZ point, SupplyConnParameter fcParam)
		{
			if (pipe == null)
			{
				return;
			}
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			Connector connector = hypiping.StartConnector.Connector;
			if (origin.DistanceTo(point) > origin2.DistanceTo(point))
			{
				connector = hypiping.EndConnector.Connector;
				origin = hypiping.EndConnector.Origin;
				origin2 = hypiping.StartConnector.Origin;
			}
			XYZ pipeVector = origin.Subtract(origin2).Normalize();
			double num = 0.0;
			FamilySymbol familySymbolByName = this.GetFamilySymbolByName("上接水角阀", "Accessory", ref this.operationMSG);
			point = new XYZ(point.X, point.Y, this.fiDesign.GetElevation(this.doc, this.valveOffset));
			FamilyInstance familyInstance = this.LayFamilyInstance(familySymbolByName, point);
			FamilyInstance fiFixture = fcParam.Instance as FamilyInstance;
			this.SetFamilyInstance(familyInstance, fiFixture);
			XYZ point2 = (familyInstance.Location as LocationPoint).Point;
			Connector connector2 = this.GetConnector(familyInstance, new XYZ(-1.0, 0.0, 0.0));
			if (this.mainPipeOffset != this.valveOffset)
			{
				num = this.mainPipeOffset - this.valveOffset;
				if (num < 0.0)
				{
					this.hyRotate.RotateElem(familyInstance, point2, new XYZ(-1.0, 0.0, 0.0), 180.0);
				}
				this.RotateAngleValve(pipeVector, familyInstance, point2, 0);
				if (num < 0.0)
				{
					connector2 = this.GetConnector(familyInstance, new XYZ(0.0, 0.0, -1.0));
					point = new XYZ(point.X, point.Y, point.Z - 0.098425196850393692);
				}
				else
				{
					connector2 = this.GetConnector(familyInstance, new XYZ(0.0, 0.0, 1.0));
					point = new XYZ(point.X, point.Y, point.Z + 0.098425196850393692);
				}
				Pipe pipe2 = this.CreatSidePipe(point, connector.Origin, this.sideDiameter);
				this.hyRotate.MovePiping(pipe2);
				new YJKPipeConnectorHelper().creatELBOW(pipe, pipe2, connector.Origin, this.doc);
				YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe2);
				connector = hypiping2.StartConnector.Connector;
				XYZ origin3 = hypiping2.StartConnector.Connector.Origin;
				XYZ origin4 = hypiping2.EndConnector.Connector.Origin;
				if (origin3.DistanceTo(point) > origin4.DistanceTo(point))
				{
					connector = hypiping2.EndConnector.Connector;
				}
			}
			if (num == 0.0)
			{
				this.RotateAngleValve(pipeVector, familyInstance, point2, 1);
			}
			XYZ xyz = connector.Origin.Subtract(connector2.Origin);
			ElementTransformUtils.MoveElement(this.doc, familyInstance.Id, xyz);
			connector.ConnectTo(connector2);
		}

		private FamilyInstance LayAngleValue(XYZ layPnt, Element fi, XYZ rorateVec, ref Connector angleConn, bool bIsUn)
		{
			FamilySymbol familySymbolByName = this.GetFamilySymbolByName("上接水角阀", "Accessory", ref this.operationMSG);
			FamilyInstance familyInstance = this.LayFamilyInstance(familySymbolByName, layPnt);
			FamilyInstance fiFixture = fi as FamilyInstance;
			this.SetFamilyInstance(familyInstance, fiFixture);
			angleConn = this.GetConnector(familyInstance, new XYZ(-1.0, 0.0, 0.0));
			XYZ xyz = angleConn.CoordinateSystem.BasisZ * -1.0;
			XYZ xyz2 = xyz.CrossProduct(rorateVec).Normalize();
			if (xyz2.IsAlmostEqualTo(new XYZ(0.0, 0.0, 0.0), 0.0001))
			{
				xyz2 = new XYZ(0.0, 0.0, 1.0);
			}
			double twoVectorAngle = YJKRevitTools.GetTwoVectorAngle(xyz, rorateVec, xyz2);
			this.hyRotate.RotatesElem(familyInstance, layPnt, xyz2, twoVectorAngle);
			this.doc.Regenerate();
			if (!bIsUn)
			{
				this.hyRotate.RotatesElem(familyInstance, layPnt, rorateVec, Math.PI);
			}
			this.doc.Regenerate();
			return familyInstance;
		}

		private Pipe ConnectAngleValue(Pipe pipe, SupplyConnParameter fcParam, bool beChange = true)
		{
			Pipe pipe2 = null;
			XYZ xyz = fcParam.LayPoint;
			XYZ endXYZ = fcParam.TurnPoint;
			if (!beChange)
			{
				xyz = new XYZ(fcParam.ProPoint.X, fcParam.ProPoint.Y, fcParam.LayPoint.Z);
				endXYZ = fcParam.ProPoint;
			}
			FamilySymbol familySymbolByName = this.GetFamilySymbolByName("上接水角阀", "Accessory", ref this.operationMSG);
			FamilyInstance familyInstance = this.LayFamilyInstance(familySymbolByName, xyz);
			FamilyInstance familyInstance2 = fcParam.Instance as FamilyInstance;
			this.SetFamilyInstance(familyInstance, familyInstance2);
			XYZ pipeVector = familyInstance2.FacingOrientation * -1.0;
			Connector connector = this.GetConnector(familyInstance, new XYZ(-1.0, 0.0, 0.0));
			double num = xyz.Z - fcParam.ProPoint.Z;
			if (Math.Abs(num) < 0.32808398950131235)
			{
				return pipe2;
			}
			if (num > 0.0)
			{
				this.hyRotate.RotateElem(familyInstance, xyz, new XYZ(-1.0, 0.0, 0.0), 180.0);
			}
			this.RotateAngleValve(pipeVector, familyInstance, xyz, 0);
			if (num > 0.0)
			{
				connector = this.GetConnector(familyInstance, new XYZ(0.0, 0.0, -1.0));
			}
			else
			{
				connector = this.GetConnector(familyInstance, new XYZ(0.0, 0.0, 1.0));
			}
			pipe2 = YJKRevitTools.NewPipe(pipe, connector, endXYZ);
			this.doc.Regenerate();
			this.hyRotate.MovePiping(pipe2);
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe2);
			Connector connector2 = hypiping.StartConnector.Connector;
			if (connector.Origin.IsAlmostEqualTo(hypiping.EndConnector.Origin, 0.0001))
			{
				connector2 = hypiping.EndConnector.Connector;
			}
			connector2.ConnectTo(connector);
			return pipe2;
		}

		private void ConnAngleValve(Pipe pipe, SupplyConnParameter fcParam, XYZ layPnt)
		{
			if (pipe == null)
			{
				return;
			}
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			Connector connector = hypiping.StartConnector.Connector;
			if (origin.DistanceTo(layPnt) > origin2.DistanceTo(layPnt))
			{
				Connector connector2 = hypiping.EndConnector.Connector;
				origin = hypiping.EndConnector.Origin;
				origin2 = hypiping.StartConnector.Origin;
			}
			FamilySymbol familySymbolByName = this.GetFamilySymbolByName("上接水角阀", "Accessory", ref this.operationMSG);
			FamilyInstance familyInstance = this.LayFamilyInstance(familySymbolByName, layPnt);
			FamilyInstance familyInstance2 = fcParam.Instance as FamilyInstance;
			this.SetFamilyInstance(familyInstance, familyInstance2);
			XYZ pipeVector = familyInstance2.FacingOrientation * -1.0;
			XYZ point = (familyInstance.Location as LocationPoint).Point;
			this.GetConnector(familyInstance, new XYZ(-1.0, 0.0, 0.0));
			double num = origin.Z - origin2.Z;
			if (num > 0.0)
			{
				this.hyRotate.RotateElem(familyInstance, point, new XYZ(-1.0, 0.0, 0.0), 180.0);
			}
			this.RotateAngleValve(pipeVector, familyInstance, point, 0);
			if (num > 0.0)
			{
				this.GetConnector(familyInstance, new XYZ(0.0, 0.0, -1.0));
				layPnt = new XYZ(layPnt.X, layPnt.Y, layPnt.Z - 0.098425196850393692);
				return;
			}
			this.GetConnector(familyInstance, new XYZ(0.0, 0.0, 1.0));
			layPnt = new XYZ(layPnt.X, layPnt.Y, layPnt.Z + 0.098425196850393692);
		}

		private void ConnAngleValve(Pipe pipe, SupplyConnParameter fcParam)
		{
			if (pipe == null)
			{
				return;
			}
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			Pipe pipe2 = null;
			if (fcParam.LayPoint.DistanceTo(fcParam.TurnPoint) > 0.16404199475065617)
			{
				pipe2 = this.CreatSidePipe(fcParam.LayPoint, fcParam.TurnPoint, this.sideDiameter);
				this.hyRotate.MovePiping(pipe2);
			}
			if (pipe2 == null)
			{
				return;
			}
			new HYPipeConnect().NewElbowFitting(new List<Pipe>
			{
				pipe,
				pipe2
			}, true);
			this.doc.Regenerate();
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe2);
			XYZ origin = hypiping2.StartConnector.Origin;
			XYZ origin2 = hypiping2.EndConnector.Origin;
			Connector connector = hypiping2.StartConnector.Connector;
			if (origin.DistanceTo(fcParam.LayPoint) > origin2.DistanceTo(fcParam.LayPoint))
			{
				Connector connector2 = hypiping.EndConnector.Connector;
				origin = hypiping.EndConnector.Origin;
				origin2 = hypiping.StartConnector.Origin;
			}
			XYZ pipeVector = (fcParam.Instance as FamilyInstance).FacingOrientation * -1.0;
			FamilySymbol familySymbolByName = this.GetFamilySymbolByName("上接水角阀", "Accessory", ref this.operationMSG);
			FamilyInstance familyInstance = this.LayFamilyInstance(familySymbolByName, fcParam.LayPoint);
			FamilyInstance fiFixture = fcParam.Instance as FamilyInstance;
			this.SetFamilyInstance(familyInstance, fiFixture);
			XYZ point = (familyInstance.Location as LocationPoint).Point;
			this.GetConnector(familyInstance, new XYZ(-1.0, 0.0, 0.0));
			double num = origin.Z - origin2.Z;
			if (num > 0.0)
			{
				this.hyRotate.RotateElem(familyInstance, point, new XYZ(-1.0, 0.0, 0.0), 180.0);
			}
			this.RotateAngleValve(pipeVector, familyInstance, point, 0);
			XYZ xyz = fcParam.LayPoint;
			if (num > 0.0)
			{
				this.GetConnector(familyInstance, new XYZ(0.0, 0.0, -1.0));
				xyz = new XYZ(xyz.X, xyz.Y, xyz.Z - 0.098425196850393692);
				return;
			}
			this.GetConnector(familyInstance, new XYZ(0.0, 0.0, 1.0));
			xyz = new XYZ(xyz.X, xyz.Y, xyz.Z + 0.098425196850393692);
		}

		private void RotateAngleValve(XYZ pipeVector, FamilyInstance fi, XYZ layPnt, int rotateStyle)
		{
			Connector connector = this.GetConnector(fi, new XYZ(-1.0, 0.0, 0.0));
			XYZ xyz = connector.CoordinateSystem.BasisZ;
			if (rotateStyle == 1)
			{
				xyz = connector.CoordinateSystem.BasisZ * -1.0;
			}
			Line line = Line.CreateBound(layPnt, layPnt + new XYZ(0.0, 0.0, 1.0));
			line.MakeUnbound();
			double num = pipeVector.AngleTo(xyz);
			if (num > 0.0001)
			{
				ElementTransformUtils.RotateElement(this.doc, fi.Id, line, num);
				this.doc.Regenerate();
				if (rotateStyle == 1)
				{
					if (!pipeVector.IsAlmostEqualTo(connector.CoordinateSystem.BasisZ * -1.0))
					{
						ElementTransformUtils.RotateElement(this.doc, fi.Id, line, Math.PI*2 - 2.0 * num);
						this.doc.Regenerate();
						return;
					}
				}
				else if (!pipeVector.IsAlmostEqualTo(connector.CoordinateSystem.BasisZ))
				{
					ElementTransformUtils.RotateElement(this.doc, fi.Id, line, Math.PI*2 - 2.0 * num);
					this.doc.Regenerate();
				}
			}
		}

		private Connector GetConnector(FamilyInstance fi, XYZ vector)
		{
			Connector result = null;
			foreach (Connector connector in ((IEnumerable<Connector>)YJKRevitTools.GetElementAllConnector(fi)))
			{
				if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(vector, 0.0001))
				{
					result = connector;
				}
			}
			return result;
		}

		private FamilyInstance LayFamilyInstance(FamilySymbol fs, XYZ point)
		{
			FamilyInstance familyInstance = YJKRevitTools.LayFamilySymbol(this.doc, point, fs, 0.0);
			this.doc.Regenerate();
			this.hyRotate.MoveFamilyInstance(familyInstance, new XYZ(0.0, 1.0, 0.0) * 0.001);
			if (!familyInstance.GetParameter("入口直径").IsReadOnly)
			{
				familyInstance.GetParameter("入口直径").Set(this.sideDiameter / 304.8);
			}
			else if (!familyInstance.GetParameter("出口直径").IsReadOnly)
			{
				familyInstance.GetParameter("出口直径").Set(this.sideDiameter / 304.8);
			}
			this.doc.Regenerate();
			return familyInstance;
		}

		private void SetFamilyInstance(FamilyInstance fiValve, FamilyInstance fiFixture)
		{
			string paramName = "CWFU";
			string paramName2 = "流量";
			string paramName3 = "设备名称";
			string text = "对应卫浴";
			SetSanitaryData setSanitaryData = YJKRevitTools.GetSetSanitaryData(this.doc, fiFixture);
			Parameter parameter = fiValve.GetParameter(paramName);
			if (parameter != null && !parameter.IsReadOnly)
			{
				parameter.Set(setSanitaryData.FeedEquivalent);
			}
			parameter = fiValve.GetParameter(paramName2);
			if (parameter != null && !parameter.IsReadOnly)
			{
				parameter.Set(setSanitaryData.FeedFlow);
			}
			parameter = fiValve.GetParameter(paramName3);
			if (parameter != null && !parameter.IsReadOnly)
			{
				parameter.Set(setSanitaryData.SanitaryType);
			}
			if (fiValve.GetParameter(text) == null)
			{
				YJKRevitTools.SetInstanceParameter(this.uiApp, Path.Combine(Product.DataLocation, "HYShared Parameters.txt"), "卫浴参数", text, BuiltInCategory.OST_PipeAccessory, false);
			}
			parameter = fiValve.GetParameter(text);
			if (parameter != null && !parameter.IsReadOnly)
			{
				parameter.Set(fiFixture.Symbol.Family.Name);
			}
			this.doc.Regenerate();
		}

		private FamilySymbol GetFamilySymbolByName(string fsName, string fileName, ref string loadMSG)
		{
			FamilySymbol familySymbol = null;
			if (string.IsNullOrEmpty(fsName))
			{
				loadMSG = "族名不能为空！";
				return familySymbol;
			}
			familySymbol = YJKRevitTools.GetFamilySymbol(fsName, this.doc);
			if (familySymbol == null)
			{
				string text = Path.Combine(Product.FamilyLocation, RevitVersion.Version, fileName, fsName + ".rfa");
				if (!File.Exists(text))
				{
					loadMSG = "族不存在或者加载地址不正确！";
					return familySymbol;
				}
				SubTransaction subTransaction = new SubTransaction(this.doc);
				subTransaction.Start();
				Family family = null;
				this.doc.LoadFamily(text, out family);
				if (family == null)
				{
					subTransaction.RollBack();
					return null;
				}
				familySymbol = YJKRevitTools.GetFamilySymbol(fsName, this.doc);
				subTransaction.Commit();
			}
			return familySymbol;
		}

		private XYZ GetPipeMidPnt(Pipe pipe)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			return origin.Add(origin2) / 2.0;
		}

		private XYZ GetPipePoint(Pipe pipe, XYZ basePnt)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			XYZ result;
			if (origin.DistanceTo(basePnt) > origin2.DistanceTo(basePnt))
			{
				result = origin2;
			}
			else
			{
				result = origin;
			}
			return result;
		}

		private bool CheckOutPointDirect(SetSanitaryData para)
		{
			return para.KeyTerminal == "YSZB" || para.KeyTerminal == "SLHYLT" || para.KeyTerminal == "SHLHYLT" || para.KeyTerminal == "LYPT1" || para.KeyTerminal == "JTSPT" || para.KeyTerminal == "HWGY2" || (!(para.KeyTerminal == "HWGY1") && !(para.KeyTerminal == "DBQZBSCXF") && !(para.KeyTerminal == "JTSHSPT") && !(para.KeyTerminal == "JTSDBQ") && para.PointOffset < 0.5);
		}

		private List<SupplyConnParameter> InitialParameter(Pipe pipe, XYZ basePnt, XYZ vector, IList<FamilyInstance> listFi, ref int notDefNum)
		{
			notDefNum = 0;
			List<SupplyConnParameter> list = new List<SupplyConnParameter>();
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			YJKLine hyline = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
			hyline.MakeUnBound();
			for (int i = 0; i < listFi.Count; i++)
			{
				SupplyConnParameter supplyConnParameter = new SupplyConnParameter();
				supplyConnParameter.Instance = listFi[i];
				SetSanitaryData setSanitaryData = YJKRevitTools.GetSetSanitaryData(this.doc, listFi[i]);
				XYZ xyz = setSanitaryData.ColdPoint;
				if (this.beHotPipe)
				{
					xyz = setSanitaryData.HotPoint;
				}
				if (xyz == null)
				{
					notDefNum++;
				}
				else
				{
					supplyConnParameter.LayPoint = xyz;
					supplyConnParameter.TurnPoint = new XYZ(xyz.X, xyz.Y, basePnt.Z);
					supplyConnParameter.ProPoint = hyline.GetProject(supplyConnParameter.TurnPoint);
					supplyConnParameter.DistanceOfEnd = supplyConnParameter.ProPoint.DistanceTo(basePnt);
					supplyConnParameter.HorizontalVector = supplyConnParameter.TurnPoint.Subtract(supplyConnParameter.ProPoint);
					supplyConnParameter.BIsUp = this.CheckOutPointDirect(setSanitaryData);
					XYZ xyz2 = supplyConnParameter.ProPoint.Subtract(basePnt).Normalize();
					if (xyz2.IsAlmostEqualTo(vector, 0.0001) || xyz2.IsAlmostEqualTo(new XYZ(0.0, 0.0, 0.0), 0.0001))
					{
						list.Add(supplyConnParameter);
					}
				}
			}
			List<SupplyConnParameter> list2 = (from c in list
			orderby c.DistanceOfEnd
			select c).ToList<SupplyConnParameter>();
			int num = 1;
			for (int j = 0; j < list2.Count; j++)
			{
				if (j == 0)
				{
					list2[j].IndexOfPipe = num;
				}
				else if (Math.Abs(list2[j].DistanceOfEnd - list2[j - 1].DistanceOfEnd) < 0.032808398950131233)
				{
					list2[j].IndexOfPipe = num;
				}
				else
				{
					num++;
					list2[j].IndexOfPipe = num;
				}
			}
			return list2;
		}

		public IList<List<SupplyConnParameter>> OrderByFanCoilOfVector(IList<SupplyConnParameter> listParam)
		{
			IList<List<SupplyConnParameter>> list = new List<List<SupplyConnParameter>>();
			XYZ xyz = null;
			List<SupplyConnParameter> list2 = new List<SupplyConnParameter>();
			List<SupplyConnParameter> list3 = new List<SupplyConnParameter>();
			foreach (SupplyConnParameter supplyConnParameter in listParam)
			{
				bool flag = false;
				if (supplyConnParameter.HorizontalVector != null)
				{
					if (xyz == null)
					{
						xyz = supplyConnParameter.HorizontalVector.Normalize();
					}
					double length = supplyConnParameter.HorizontalVector.GetLength();
					if (list2.Count < 1)
					{
						list2.Add(supplyConnParameter);
					}
					else if (xyz.IsAlmostEqualTo(supplyConnParameter.HorizontalVector.Normalize(), 0.01))
					{
						foreach (SupplyConnParameter supplyConnParameter2 in list2)
						{
							if (supplyConnParameter2.HorizontalVector.GetLength() < length)
							{
								list2.Insert(list2.IndexOf(supplyConnParameter2), supplyConnParameter);
								flag = true;
								break;
							}
						}
						if (!flag)
						{
							list2.Add(supplyConnParameter);
						}
					}
					else if (list3.Count < 1)
					{
						list3.Add(supplyConnParameter);
					}
					else
					{
						foreach (SupplyConnParameter supplyConnParameter3 in list3)
						{
							if (supplyConnParameter3.HorizontalVector.GetLength() < length)
							{
								list3.Insert(list3.IndexOf(supplyConnParameter3), supplyConnParameter);
								flag = true;
								break;
							}
						}
						if (!flag)
						{
							list3.Add(supplyConnParameter);
						}
					}
				}
			}
			int num = 1;
			for (int i = 0; i < list2.Count; i++)
			{
				list2[i].IndexOfElement = num;
				num++;
			}
			num = 1;
			for (int j = 0; j < list3.Count; j++)
			{
				list3[j].IndexOfElement = num;
				num++;
			}
			if (list2.Count > 0)
			{
				list.Add(list2);
			}
			if (list3.Count > 0)
			{
				list.Add(list3);
			}
			return list;
		}

		private XYZ GetPipeVector(ref XYZ startPnt, ref XYZ endPnt, YJKPiping<Pipe> hyPiping)
		{
			startPnt = hyPiping.StartConnector.Origin;
			endPnt = hyPiping.EndConnector.Origin;
			XYZ result;
			if (startPnt.DistanceTo(this.originalPnt) > endPnt.DistanceTo(this.originalPnt))
			{
				result = startPnt.Subtract(endPnt).Normalize();
				this.originalPnt = endPnt;
			}
			else
			{
				result = endPnt.Subtract(startPnt).Normalize();
				this.originalPnt = startPnt;
			}
			return result;
		}

		public bool IsHorFirst { get; set; }

		private Pipe basePipe;

		private List<FamilyInstance> listFI;

		private bool connStyle;

		private bool beDraw;

		private bool beAdd;

		private double mainPipeOffset;

		private double valveOffset;

		private double diameter;

		private double sideDiameter;

		private double angle;

		private XYZ originalPnt;

		private PlumbSystemAutoDesign fiDesign;

		private Pipe mainPipe;

		private XYZ baseVector;

		private double moveDistance;

		public string operationMSG;

		private UIApplication uiApp;

		private Document doc;

		private PipingSystemType systemType;

		private PipeType pipeType;

		private YJKRotateFamilyInstance hyRotate = new YJKRotateFamilyInstance();

		public bool checkPVDistance = true;

		private bool beHotPipe;

		private int sanitaryNum;
	}
}
